/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.Unit;
import org.crystalrange.enterprise.service.UnitService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class UnitListController extends BaseController {

    protected UnitService unitService;
    protected Unit selectedUnit;
    protected Listbox listboxUnit;
    protected Search searchUnit;
    protected List<Unit> units;

    public UnitService getUnitService() {
        return this.unitService;
    }

    public void setUnitService(UnitService unitService) {
        this.unitService = unitService;
    }

    public Unit getSelectedUnit() {
        return this.selectedUnit;
    }

    public void setSelectedUnit(Unit selectedUnit) {
        this.selectedUnit = selectedUnit;
    }

    public List<Unit> getUnits() {
        return this.units;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchUnit = new Search(Unit.class);
        this.searchUnit.setMaxResults(100);
        this.searchUnit.addFetch("createdBy");
        this.searchUnit.addFetch("updatedBy");
        this.units = this.unitService.search(this.searchUnit);

        component.setAttribute("controller", this, false);
    }

    public void onClick$menuitemUnitCreate(Event event) throws Exception {
        this.unitCreate();
    }

    public void onClick$menuitemUnitEdit(Event event) throws Exception {
        this.unitEdit();
    }

    public void onClick$menuitemUnitDelete(Event event) throws Exception {
        this.unitDelete();
    }

    public void onClick$menuitemUnitSearch(Event event) throws Exception {
        this.unitSearch();
    }

    public void onClick$menuitemUnitRefresh(Event event) throws Exception {
        this.unitRefresh();
    }

    public void onClick$toolbarbuttonUnitCreate(Event event) throws Exception {
        this.unitCreate();
    }

    public void onClick$toolbarbuttonUnitEdit(Event event) throws Exception {
        this.unitEdit();
    }

    public void onClick$toolbarbuttonUnitDelete(Event event) throws Exception {
        this.unitDelete();
    }

    public void onClick$toolbarbuttonUnitSearch(Event event) throws Exception {
        this.unitSearch();
    }

    public void onClick$toolbarbuttonUnitRefresh(Event event) throws Exception {
        this.unitRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.unitEdit();
    }
    
    private void unitCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/unit.zul");
    }

    private void unitEdit() throws Exception {
        if (this.selectedUnit != null) {
            this.execution.sendRedirect("~./zul/html/unit.zul?Id=" + this.selectedUnit.getId().toString());
        }
    }

    private void unitDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxUnit.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Unit unit = (Unit) selectedItem.getValue();
                    this.unitService.remove(unit);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.units = this.unitService.search(this.searchUnit);
        this.binder.loadAll();
    }

    private void unitSearch() throws Exception {
        this.execution.setAttribute("searchUnit", this.searchUnit);

        Window window = (Window) this.execution.createComponents("~./zul/html/unitSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchUnit = (Search) this.execution.getAttribute("searchUnit");
        }

        this.units = this.unitService.search(this.searchUnit);
        this.binder.loadAll();
    }

    private void unitRefresh() {
        this.units = this.unitService.search(this.searchUnit);
        this.binder.loadAll();
    }
}
